/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package z.h.w.jar.data.time.period;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import z.h.w.jar.data.time.Time;
import z.h.w.jar.data.time.Unit;

/**
 *
 * @author EnzoZhong
 */
public abstract class Eyewink implements Lastable {

   private Time start;
   private Time end;

   /**
    * 
    * @param start
    * @param end
    */
   public Eyewink ( Long start , Long end ) {
      Long time1 = start;
      Long time2 = end;
      if ( time1 > time2 ) {
         this.start = Time.born( end );
         this.end = Time.born( start );
      } else {
         this.start = Time.born( start );
         this.end = Time.born( end );
      }
   }

   /**
    * 
    * @param arg
    * @param unit
    */
   public Eyewink ( Double arg , Unit unit ) {
      Time now = Time.born();
      this.start = now.offset( -arg , unit );
      this.end = now.offset( arg , unit );
   }

   /**
    * 
    * @param start
    * @param end
    */
   public Eyewink ( Date start , Date end ) {
      this( start.getTime() , end.getTime() );
   }

   /**
    *
    * @param start end
    * @param end  
    */
   public Eyewink ( Time start , Time end ) {
      this( start.getTime() , end.getTime() );
   }

   /**
    *
    * @param sillabare
    */
   public Eyewink ( Eyewink sillabare ) {
      this( sillabare.getStart() , sillabare.getEnd() );
   }

   /**
    *
    * @param lastable
    */
   public Eyewink ( Lastable lastable ) {
      this( lastable.getStart() , lastable.getEnd() );
   }

   /**
    *
    * @param start am multiple unit
    * @param multiple
    * @param unit  
    */
   public Eyewink ( Time start , Double multiple , Unit unit ) {
      this.start = start;
      Long time = start.getTime() + unit.convert( multiple ).longValue();
      this.end = Time.born( time );
   }

   /**
    *
    * @return
    */
   @Override
   public Time getEnd () {
      return end;
   }

   /**
    *
    * @param end
    */
   public void setEnd ( Time end ) {
      this.end = end;
   }

   /**
    *
    * @return
    */
   @Override
   public Time getStart () {
      return start;
   }

   /**
    *
    * @param start
    */
   public void setStart ( Time start ) {
      this.start = start;
   }

   /**
    *
    * @return
    */
   public Long getTime () {
      return end.getTime() - start.getTime();
   }

   /**
    *
    * @return
    */
   public List<Time> toList () {
      List<Time> list = new ArrayList();
      list.add( start );
      list.add( end );
      return list;
   }

   /**
    *
    * @return
    */
   public Time[] toArray () {
      return new Time[] { start , end };
   }

   @Override
   public String toString () {
      return "【" + start + "、p:" + getTime() + "、" + end + '】';
   }
}
